import { defineStore } from 'pinia'
import { ref } from 'vue'

// 定义 Store
export const useWorksubStore = defineStore(
  'worksub',
  () => {
    // records数据
    const records = ref({})

    // 设置records数据
    const setRecords = (work_main_id, value) => {
      records.value[work_main_id] = value
    }
    // const setRecords = computed((work_main_id, value) => {
    //   records.value[work_main_id] = value
    //   return records.value
    // })

    const clearRecords = () => {
      records.value = {}
    }

    // 当前work_main_id
    const currentWorkMainId = ref()
    // 设置当前work_main_id
    const setCurrentWorkMainId = (value) => {
      currentWorkMainId.value = value
    }
    // 清空当前work_main_id
    const ClearCurrentWorkMainId = (value) => {
      currentWorkMainId.value = value
    }
    const deleteRecord = (work_main_id) => {
      // console.log('subrec', work_main_id)
      records.value[work_main_id] = undefined
    }

    // 记得 return
    return {
      records,
      setRecords,
      currentWorkMainId,
      setCurrentWorkMainId,
      clearRecords,
      ClearCurrentWorkMainId,
      deleteRecord,
    }
  },
  {
    // 网页端配置
    // persist: true,
    // 小程序端配置
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key)
        },
        setItem(key, value) {
          return uni.setStorageSync(key, value)
        },
      },
    },
  },
)
